from django.utils.decorators import method_decorator
from django.http import HttpResponse

from gis.common.django_ext.decorators import validate_parameters
from gis.common.django_ext.views import BaseView, PaginationResponse
from gis.admin.decorators import require_login, check_permission
from itpay.core.agency import transation_service
from itpay.core.agency.const import AgencyType
from itpay.core.mch import const as mch_const
from itpay.server_admin.decorators import require_vcode
from itpay.server_admin.view.agency.agency_recharge_forms import (
    ListAgencyRechargeListSchema,
    RechargeReportListSchema,
    TtlRechargeReportSchema,
    AgencyReportExportSchema,
    AgencyStatsReportSchema,
    InnerAgencyStatsReportSchema,
    AgencyOrderChangeStatusSchema,
)
from itpay.utils import export_file
from .helper import wrapper_agency_name
from itpay.core.sys import export_service, const as export_const


class AgencyRechargeOrderView(BaseView):
    @method_decorator(require_login)
    @method_decorator(
        check_permission(["agency_admin_recharge_get", "agency_recharge_get"])
    )
    @method_decorator(validate_parameters(ListAgencyRechargeListSchema))
    def get(self, request, cleaned_data):
        if cleaned_data.get("export"):
            del cleaned_data["export"]
            export_type = (
                export_const.ExportType.ADMIN_AGENCY_RECHARGE
                if cleaned_data["agency_type"] == AgencyType.ADMIN_AGENCY
                else export_const.ExportType.NORMAL_AGENCY_RECHARGE
            )
            task = export_service.create_export_task(cleaned_data, export_type)
            return dict(task_id=task["id"])
        total, order = transation_service.list_apply_recharge(**cleaned_data)
        return PaginationResponse(total, [wrapper_agency_name(e) for e in order])


class AgencyReportView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission("agency_report_get"))
    @method_decorator(validate_parameters(RechargeReportListSchema))
    def get(self, request, cleaned_data):
        total, resp_items = transation_service.list_agency_recharge_report(
            **cleaned_data
        )
        return PaginationResponse(total, resp_items)


class AgencyDailyReportView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission("agency_report_get"))
    @method_decorator(validate_parameters(TtlRechargeReportSchema))
    def get(self, request, cleaned_data):
        resp_items = transation_service.get_daily_report(**cleaned_data)
        return resp_items


class AgencyReportExportView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission("agency_report_export"))
    @method_decorator(validate_parameters(AgencyReportExportSchema))
    def get(self, request, cleaned_data):
        filename = export_file.gen_filename("agency_report")
        data = transation_service.agency_report_export(**cleaned_data)
        response = HttpResponse()
        response["Content-Type"] = "application/octet-stream"
        response["Content-Disposition"] = "attachment;filename={}".format(filename)
        response.write(data)
        return response


class AgencyStatsReportView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission("agency_report_get"))
    @method_decorator(validate_parameters(AgencyStatsReportSchema))
    def get(self, request, cleaned_data):
        cleaned_data["mch_type"] = "[]".format(mch_const.MchType.INNER)
        if cleaned_data.get("export"):
            del cleaned_data["export"]
            task = export_service.create_export_task(
                cleaned_data, export_const.ExportType.AGENCY_STATS
            )
            return dict(task_id=task["id"])
        total, resp_items = transation_service.get_agency_stats_report(**cleaned_data)
        return PaginationResponse(total, resp_items)


class InnerAgencyStatsReportView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission("inner_agency_report_get"))
    @method_decorator(validate_parameters(InnerAgencyStatsReportSchema))
    def get(self, request, cleaned_data):
        cleaned_data["parent_ids"] = "[]"
        cleaned_data["agency_ids"] = "[]"
        cleaned_data["mch_type"] = "[{}]".format(mch_const.MchType.INNER)
        if cleaned_data.get("export"):
            del cleaned_data["export"]
            task = export_service.create_export_task(
                cleaned_data, export_const.ExportType.AGENCY_STATS
            )
            return dict(task_id=task["id"])
        total, resp_items = transation_service.get_agency_stats_report(**cleaned_data)
        return PaginationResponse(total, resp_items)


class AgencyRechargeAgreeView(BaseView):
    @method_decorator(require_login)
    @method_decorator(require_vcode)
    @method_decorator(validate_parameters(AgencyOrderChangeStatusSchema))
    @method_decorator(check_permission("agency_admin_recharge_agree"))
    def put(self, request, order_id, cleaned_data):
        transation_service.agree_recharge(order_id, cleaned_data["reason"])
        return dict(id=order_id)


class AgencyRechargeRefuseView(BaseView):
    @method_decorator(require_login)
    @method_decorator(require_vcode)
    @method_decorator(validate_parameters(AgencyOrderChangeStatusSchema))
    @method_decorator(check_permission("agency_admin_recharge_refuse"))
    def put(self, request, order_id, cleaned_data):
        transation_service.refuse_agency_recharge(order_id, cleaned_data["reason"])
        return dict(id=order_id)


class AgencyChaseAgreeView(BaseView):
    @method_decorator(require_login)
    @method_decorator(require_vcode)
    @method_decorator(validate_parameters(AgencyOrderChangeStatusSchema))
    @method_decorator(check_permission("agency_admin_recharge_chase"))
    def put(self, request, order_id, cleaned_data):
        transation_service.chase_recharge(order_id, cleaned_data["reason"])
        return dict(id=order_id)
